/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.kramerius.rukopisy.tools.cli;

import cz.mzk.kramerius.commons.Dom4jUtils;
import cz.mzk.kramerius.fedoraaccess.FedoraConnector;
import cz.mzk.kramerius.fedoraaccess.FedoraServer;
import cz.mzk.kramerius.fedoraaccess.rest.RESTConnector;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;

/**
 *
 * @author Martin Řehánek <rehan at mzk.cz>
 *
 */
public class ImportFoxmlsAction implements Action {

    private static final Logger logger = Logger.getLogger(ImportFoxmlsAction.class.getName());
    private final File foxmlFilesList;
    private final File properties;

    /**
     *
     * @param properties
     * @param foxmlFilesList file that contains on each line whole filename of foxml file to ingest.
     * Each foxml file has to be named $UUID.foxml i.e. so the whole line is
     * /path/to/file/$UUID.foxml, i.e.
     * /home/rehan/rukopisy/ENRICH/converted/MK_Mikulov/Mk-0000.062/b75b5a23-7d9a-4518-b726-13e9707f0ca1.xml
     */
    public ImportFoxmlsAction(File properties, File foxmlFilesList) {
        this.properties = properties;
        this.foxmlFilesList = foxmlFilesList;
    }

    public void run() throws Exception {
        FedoraConnector con = initConnector(properties);
        List<File> filesToIngest = loadFilesToIngest(foxmlFilesList);
        ingestFiles(con, filesToIngest);
    }

    private FedoraConnector initConnector(File properties) throws IOException {
        FedoraServer fedoraServer = new FedoraServer(properties);
        return new RESTConnector(fedoraServer);
    }

    private List<File> loadFilesToIngest(File filesList) throws IOException {
        LineNumberReader reader = new LineNumberReader(new FileReader(filesList));
        List<File> result = new ArrayList<File>();
        while (reader.ready()) {
            String fileName = reader.readLine();
            File file = new File(fileName);
            result.add(file);
        }
        return result;
    }

    private void ingestFiles(FedoraConnector con, List<File> filesToIngest) throws DocumentException, FileNotFoundException {
        for (File file : filesToIngest) {
            String filename = file.getName();
            String pid = "uuid:" + filename.substring(0, filename.length() - ".xml".length());
            if (con.objectExists(pid)) {
                logger.log(Level.INFO, "object {0} exists", pid);
            } else {
                logger.log(Level.INFO, "object {0} not found, ingesting", pid);
                Document foxml = Dom4jUtils.loadDocument(file, false);
                con.importFoxml(foxml, "ingesting " + pid);
            }
        }
    }
}
